package com.cm.sys.system.controller;

import java.util.HashMap;
import java.util.Map;

import com.cm.sys.framework.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.cm.sys.framework.aop.NotCheckIP;
import com.cm.sys.framework.aop.NotCheckLogin;
import com.cm.sys.framework.application.ApiResponseCode;
import com.cm.sys.framework.application.RequestParameter;
import com.cm.sys.framework.application.ResponseResult;
import com.cm.sys.framework.execption.BusinessException;
import com.cm.sys.framework.utils.JsonUtil;
import com.cm.sys.framework.utils.JwtUtils;
import com.cm.sys.system.entity.User;
import com.cm.sys.system.service.UserService;



/***
 * 登陆管理
 * 
 * @Date
 * @author yxq18
 *
 */
@Controller
public class LoginController {

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    private UserService userService;


    /***
     * 获取token
     * 
     * @param rp
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @NotCheckLogin
    @NotCheckIP
    public ResponseResult getUser(RequestParameter rp) {

        JSONObject args = rp.getparameters();

        ResponseResult rr = new ResponseResult();
        String password = JsonUtil.getString(args, "password");
        String userName = JsonUtil.getString(args, "userName");
        logger.info("获取token前端传过来的值========【{}】=============", args);
        // 验证账号密码是否正确
        User user = userService.accountUser(userName, password);
        if (StringUtils.isEmpty(user)) {
            throw new BusinessException(ApiResponseCode.PASSWORD_ERROR.getCode(),
                ApiResponseCode.PASSWORD_ERROR.getMessage());
        }
        String token = JwtUtils.createJWT(user);
        user.setToken(token);
        userService.updateUser(user);
        Map<String,Object> map = new HashMap<String,Object>(16);
        map.put("token", token);// 对接之后直接删除
        map.put("user",user);
        rr.setMap(map);
        return rr;

    }

    /***
     * 登出token清除token
     * 
     * @param rp
     * @return
     */
    @RequestMapping(value = "/logOut", method = RequestMethod.POST)
    public ResponseResult delUser(RequestParameter rp) {

        ResponseResult rr = new ResponseResult();
        JSONObject args = rp.getparameters();

        String token = rp.getToken();

        User user = JwtUtils.validateToken(token);

        logger.info("登出token前端传过来的值========【{}】==========", args);
        // 登出重置token
        userService.updateToken(user.getId(), "0");
        //设置上次登录时间
        userService.updateLoginTime(user.getId(),DateUtil.getTimestamp());
        return rr;

    }

}
